import Vue from 'vue'
import Router from 'vue-router'
const _import = require('./_import_' + process.env.NODE_ENV)
// in development env not use Lazy Loading,because Lazy Loading too many pages will cause webpack hot update too slow.so only in production use Lazy Loading

/* layout */
import Index from '../views/index/index'

Vue.use(Router)

 /**
  * icon : the icon show in the sidebar
  * hidden : if `hidden:true` will not show in the sidebar
  * redirect : if `redirect:noredirect` will not redirct in the levelbar
  * noDropdown : if `noDropdown:true` will not has submenu in the sidebar
  * meta : `{ role: ['admin'] }`  will control the page role
  *
  * 所有权限通用路由表
  * 如首页和登录页和一些不用权限的公用页面
  **/
export const constantRouterMap = [
  { path: '/login', component: _import('login/index'), hidden: true },
  { path: '/register', component: _import('register/index'), hidden: true },
  { path: '/404', component: _import('404'), hidden: true },
  {
    path: '/',
    component: Index,
    redirect: '/index',
    name: 'Index',
    hidden: true,
    children: [{ path: 'index', component: _import('index/index') }]
  }
]
// 实例化vue的时候只挂载constantRouter
export default new Router({
  // mode: 'history', //后端支持可开
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
})

// 异步挂载的路由
// 动态需要根据权限加载的路由表
export const asyncRouterMap = [
  {
    path: '/example',
    component: Index,
    redirect: 'norediret', // 重定向 path --> redirect
    name: 'Example',
    icon: 'zujian',
    children: [
      { path: 'form', name: '表单', icon: 'zonghe', component: _import('page/form') },
      {
        path: '/example/table',
        redirect: '/example/table/table',
        component: _import('layout/index'), // 多级路由需要一个rout-view
        name: 'Table',
        icon: 'table',
        children: [
          { path: 'table', name: '表格', icon: 'zonghe', component: _import('table/index'), meta: { role: ['admin'] }}
        ] },
      { path: 'img', name: '上传图片', icon: 'zonghe', component: _import('upload/uploadImg'), meta: { role: ['admin'] }},
      { path: 'char', name: '表格插件', icon: 'zonghe', component: _import('echarts/echarts'), meta: { role: ['admin'] }}
    ]
  },
  {
    path: '/table',
    component: Index,
    redirect: '/table/index',
    icon: 'tubiao',
    noDropdown: true,
    children: [{ path: 'index', name: 'Table', component: _import('table/index'), meta: { role: ['admin'] }}]
  },

  {
    path: '/sysmanager',
    component: Index,
    redirect: 'noredirect',
    name: '系统管理',
    icon: 'c',
    meta: { role: ['admin', 'sysmanager'] }, // 页面需要的权限
    children: [
      { path: 'users', name: '用户管理', icon: 'el-icon-edit', component: _import('table/index'), meta: { role: ['admin'] }},
      { path: 'roles', name: '权限管理', icon: 'zonghe', component: _import('page/form') },
      { path: 'organizes', name: '组织管理', icon: 'zonghe', component: _import('page/form') },
      { path: 'dirct', name: '数据字典', icon: 'zonghe', component: _import('page/form') }
    ]
  },
  
    {
      path: '/upload',
      component: Index, 
      redirect: 'noredirect',
      name: '上传',
      icon: 'c',
      meta: { role: ['admin', 'sysmanager'] }, // 页面需要的权限
      children: [
        { path: 'img', name: '上传图片', icon: 'el-icon-edit', component: _import('upload/uploadImg'), meta: { role: ['admin'] }},
      ]
    },

    {
        path: '/echarts',
        component: Index, 
        redirect: 'noredirect',
        name: '图表',
        icon: 'c',
        meta: { role: ['admin', 'sysmanager'] }, // 页面需要的权限
        children: [
          { path: 'char', name: '表格插件', icon: 'el-icon-edit', component: _import('echarts/echarts'), meta: { role: ['admin'] }},
        ]
      },
  // 这里有一个需要非常注意的地方就是404页面一定要最后加载，如果放在constantRouterMap一同声明了404，后面的所以页面都会被拦截到404
  { path: '*', redirect: '/404', hidden: true }
]
